import { Inject, Provide } from "@midwayjs/core";
import { InjectEntityModel } from "@midwayjs/typeorm";
import { Repository } from "typeorm";
import { Utils } from "../utils";
import { PartCreateDto, PartPageQueryDto, PartDto } from "../dto/part.dto";
import { BblPart } from "../entity/part.entity";
import { BblAttach } from "../entity/attach.entity";
import { AttachService } from "./att.service";

@Provide()
export class PartService {
  @Inject()
  utils: Utils;

  @Inject()
  attService: AttachService;

  /**
   * 附件驱动模型
   */
  @InjectEntityModel(BblPart)
  partModel: Repository<BblPart>;

  async add_part(dto: PartCreateDto) {
    const part = new BblPart();
    part.partId = this.utils.uuid();
    part.partMainName = dto.partMainName;
    part.partSubName = dto.partSubName;
    part.partPrice = dto.partPrice;
    part.partImg = new BblAttach();
    part.partImg.attId = dto.attId;
    const result = await this.partModel.save(part);
    const fileRes = await this.attService.get_att_by_id(dto.attId);
    await this.attService.tmp_to_uploads(fileRes.data);

    return this.utils.service.success(result);
  }

  async update_part(partId: string, dto: PartCreateDto) {
    const part = await this.partModel
      .createQueryBuilder("part")
      .leftJoinAndSelect("part.partImg", "partImg")
      .where("part.partId = :partId", { partId })
      .getOne();
    if (!part) {
      return this.utils.service.fail("球杆不存在");
    } else {
      part.partMainName = dto.partMainName;
      part.partSubName = dto.partSubName;
      part.partPrice = dto.partPrice;
      if (part.partImg.attId !== dto.attId) {
        this.attService.remove(part.partImg);
        const fileRes = await this.attService.get_att_by_id(dto.attId);
        part.partImg = fileRes.data;
        await this.attService.tmp_to_uploads(fileRes.data);
      }
      const result = await this.partModel.save(part);
      return this.utils.service.success(result);
    }
  }

  async delete_part(partId: string) {
    const part = await this.partModel
      .createQueryBuilder("part")
      .leftJoinAndSelect("part.partImg", "partImg")
      .where("part.partId = :partId", { partId })
      .getOne();
    if (!part) {
      return this.utils.service.fail("球杆不存在");
    } else {
      this.attService.remove(part.partImg);
      await this.partModel.remove(part);
      return this.utils.service.success(partId);
    }
  }

  async get_part_list() {
    const builder = this.partModel
      .createQueryBuilder("part")
      .leftJoinAndSelect("part.partImg", "partImg")
      .orderBy("part.createAt", "DESC");

    const parts = await builder.getMany();
    const list = parts.map((part) => new PartDto(part));

    return this.utils.service.success(list);
  }

  async get_part_page_list(dto: PartPageQueryDto) {
    const builder = this.partModel
      .createQueryBuilder("part")
      .leftJoinAndSelect("part.partImg", "partImg")
      .where("1=1")
      .orderBy("part.createAt", "DESC");

    if (dto.partMainName) {
      builder.andWhere("part.partMainName like :partMainName", {
        partMainName: `%${dto.partMainName}%`
      });
    }

    if (dto.partSubName) {
      builder.andWhere("part.partSubName like :partSubName", {
        partSubName: `%${dto.partSubName}%`
      });
    }
    const total = await builder.getCount();

    builder.skip((dto.current - 1) * dto.pageSize);
    builder.take(dto.pageSize);

    const list = await builder.getMany();
    const records = list.map((d) => new PartDto(d));

    return this.utils.service.success({
      total,
      records,
      current: Number(dto.current),
      pageSize: Number(dto.pageSize)
    });
  }
}
